package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;

import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private StatementMapper statementMapper;
    @Override
    public TablePageInfo< StatementItem > query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }

    @Override
    public void saveItems(List< StatementItem > list) {
        StatementItem item = list.remove(list.size() - 1);
        Long statementId = item.getStatementId();
        statementItemMapper.deleteItem(statementId);
        BigDecimal discountAmount = item.getItemPrice();
        BigDecimal totalAmount = new BigDecimal("0.00");
        BigDecimal totalQuantity= new BigDecimal("0");       //服务项数量
        for (StatementItem statementItem : list) {
            totalQuantity=totalQuantity.add(statementItem.getItemQuantity());
            totalAmount=totalAmount.add(statementItem.getItemQuantity().multiply(statementItem.getItemPrice()));
            statementItem.setStatementId(statementId);
           statementItemMapper.insert(statementItem);
        }
        statementMapper.updateAmcount(statementId,totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public void pay(Long statementId) {
        Statement statement = statementMapper.
                selectByPrimaryKey(statementId);
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            throw new BusinessException("非法操作");
        }
        statementMapper.updateByPay(Statement.STATUS_PAID,ShiroUtils.getUser().getId() ,statementId);
    }

    @Override
    public  Workbook exportStatement(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        BigDecimal trueAmount = statement.getTotalAmount().subtract(statement.getDiscountAmount());
        List< StatementItem > items = statementItemMapper.selectByStatementId(statementId);
        List< String > list = new ArrayList<>();
        list.add(statement.getCustomerName());
        list.add(statement.getCustomerPhone());
        list.add(statement.getActualArrivalTime().toString());
        list.add(statement.getLicensePlate());
        list.add(statement.getCarSeries());
        list.add(statement.getServiceType()==0?"维修":"保养");
        list.add(statement.getAppointmentId()==null?"否":"是");
        list.add(statement.getPayTime().toString());
        list.add(statement.getPayeeId().toString());
        list.add(statement.getTotalAmount().toString());
        list.add(statement.getDiscountAmount().toString());
        list.add(trueAmount.toString());
        list.add(statement.getTotalQuantity().toString());
        list.add(statement.getCreateTime().toString());
        list.add(statement.getInfo());
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("账单明细表");
        String[]tableHander ={"客户姓名","客户联系方式",
                "实际到店时间","车牌号码","汽车类型",
                "服务类型","是否预约","收款时间",
                "收款人id","总消费金额","折扣金额","实际支付","服务项数量","账单生成时间","备注"};
        String[]tableHander2={"服务项明细名称","服务项价格","购买数量"};
        Row row = sheet.createRow(0);
        row.createCell(tableHander.length/2).setCellValue("账单明细");
        Row r1 = sheet.createRow(1);
        Row r2 = sheet.createRow(2);
        Row r4 = sheet.createRow(4);
        Row r5 = sheet.createRow(5);
        for (int i = 0; i < tableHander.length; i++) {
            r1.createCell(i).setCellValue(tableHander[i]);
            r2.createCell(i).setCellValue(list.get(i));
        }
        r4.createCell(1).setCellValue("服务项明细");
        for (int i = 0; i < tableHander2.length; i++) {
            r5.createCell(i).setCellValue(tableHander2[i]);
        }
        for (int i = 0; i < items.size(); i++) {
            StatementItem statementItem = items.get(i);
            Row rI = sheet.createRow(i + 6);
            rI.createCell(0).setCellValue(statementItem.getItemName());
            rI.createCell(1).setCellValue(statementItem.getItemPrice().toString());
            rI.createCell(2).setCellValue(statementItem.getItemQuantity().toString());
        }
        return workbook;
    }
}
